﻿using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEditor;
using System.IO;

[CustomEditor(typeof(CharacterProperty))]
//[CanEditMultipleObjects]
public class CharacterPropertyEditor : Editor 
{
	public GameObject character;
	public CharacterProperty property;
	public Object loadtext;
	public Object exporttext;
	private SerializedObject m_object;


	void Awake()
	{
		property = (CharacterProperty)target;
		character =(GameObject)property.gameObject;
	}

	public void OnEnable()
	{
		m_object = new SerializedObject(target);
	}

	public override void OnInspectorGUI()
	{
		property = (CharacterProperty)target;
		character =(GameObject)property.gameObject;

		GUILayout.BeginVertical();
		drawstatbars();
		GUILayout.EndVertical();

		/*
		drawelementalitems();

		import = EditorGUILayout.Toggle("Import / Export Stats", import);
		if(import)
		{
			importorexport();
		}

		if (GUI.changed) 
		{
			saveelements();
		}
		*/
	}

	
	private void drawstatbars()
	{
		m_object.Update();

		character.GetComponent<CharacterProperty>().hp.Value = EditorGUILayout.IntSlider("hp",character.GetComponent<CharacterProperty>().hp.Value,0,character.GetComponent<CharacterProperty>().hpMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().hp.Value / character.GetComponent<CharacterProperty>().hpMax, "hp");
		character.GetComponent<CharacterProperty>().hpMax = EditorGUILayout.IntField("hpMax",character.GetComponent<CharacterProperty>().hpMax);
        DrawSpace();

		character.GetComponent<CharacterProperty>().hpRecovery = EditorGUILayout.IntSlider("hpRecovery",character.GetComponent<CharacterProperty>().hpRecovery,0,character.GetComponent<CharacterProperty>().hpRecoveryMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().hpRecovery / character.GetComponent<CharacterProperty>().hpRecoveryMax, "hpRecovery");
		character.GetComponent<CharacterProperty>().hpRecoveryMax = EditorGUILayout.IntField("hpRecoveryMax",character.GetComponent<CharacterProperty>().hpRecoveryMax);
        DrawSpace();

		character.GetComponent<CharacterProperty>().tp = EditorGUILayout.IntSlider("tp",character.GetComponent<CharacterProperty>().tp,0,character.GetComponent<CharacterProperty>().tpMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().tp / character.GetComponent<CharacterProperty>().tpMax, "tp");
		character.GetComponent<CharacterProperty>().tpMax = EditorGUILayout.IntField("tpMax",character.GetComponent<CharacterProperty>().tpMax);
        DrawSpace();

		character.GetComponent<CharacterProperty>().tpRecovery = EditorGUILayout.IntSlider("tpRecovery",character.GetComponent<CharacterProperty>().tpRecovery,0,character.GetComponent<CharacterProperty>().tpRecoveryMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().tpRecovery / character.GetComponent<CharacterProperty>().tpRecoveryMax, "tpRecovery");
		character.GetComponent<CharacterProperty>().tpRecoveryMax = EditorGUILayout.IntField("tpRecoveryMax",character.GetComponent<CharacterProperty>().tpRecoveryMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().sp = EditorGUILayout.IntSlider("sp",character.GetComponent<CharacterProperty>().sp,0,character.GetComponent<CharacterProperty>().spMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().sp / character.GetComponent<CharacterProperty>().spMax, "sp");
		character.GetComponent<CharacterProperty>().spMax = EditorGUILayout.IntField("spMax",character.GetComponent<CharacterProperty>().spMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().strength = EditorGUILayout.IntSlider("strength",character.GetComponent<CharacterProperty>().strength,0,character.GetComponent<CharacterProperty>().strengthMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().strength / character.GetComponent<CharacterProperty>().strengthMax, "strength");
		character.GetComponent<CharacterProperty>().strengthMax = EditorGUILayout.IntField("strengthMax",character.GetComponent<CharacterProperty>().strengthMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().strengthUp = EditorGUILayout.IntSlider("strengthUp",character.GetComponent<CharacterProperty>().strengthUp,0,character.GetComponent<CharacterProperty>().strengthUpMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().strengthUp / character.GetComponent<CharacterProperty>().strengthUpMax, "strengthUp");
		character.GetComponent<CharacterProperty>().strengthUpMax = EditorGUILayout.IntField("strengthUpMax",character.GetComponent<CharacterProperty>().strengthUpMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().magic = EditorGUILayout.IntSlider("magic",character.GetComponent<CharacterProperty>().magic,0,character.GetComponent<CharacterProperty>().magicMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().magic / character.GetComponent<CharacterProperty>().magicMax, "magic");
		character.GetComponent<CharacterProperty>().magicMax = EditorGUILayout.IntField("magicMax",character.GetComponent<CharacterProperty>().magicMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().magicUp = EditorGUILayout.IntSlider("magicUp",character.GetComponent<CharacterProperty>().magicUp,0,character.GetComponent<CharacterProperty>().magicUpMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().magicUp / character.GetComponent<CharacterProperty>().magicUpMax, "magicUp");
		character.GetComponent<CharacterProperty>().magicUpMax = EditorGUILayout.IntField("magicUpMax",character.GetComponent<CharacterProperty>().magicUpMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().defense = EditorGUILayout.IntSlider("defense",character.GetComponent<CharacterProperty>().defense,0,character.GetComponent<CharacterProperty>().defenseMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().defense / character.GetComponent<CharacterProperty>().defenseMax, "defense");
		character.GetComponent<CharacterProperty>().defenseMax = EditorGUILayout.IntField("magicUpMax",character.GetComponent<CharacterProperty>().magicUpMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().shell = EditorGUILayout.IntSlider("shell",character.GetComponent<CharacterProperty>().shell,0,character.GetComponent<CharacterProperty>().shellMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().shell / character.GetComponent<CharacterProperty>().shellMax, "shell");
		character.GetComponent<CharacterProperty>().shellMax = EditorGUILayout.IntField("shellMax",character.GetComponent<CharacterProperty>().shellMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().resist = EditorGUILayout.IntSlider("resist",character.GetComponent<CharacterProperty>().resist,0,character.GetComponent<CharacterProperty>().resistMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().resist / character.GetComponent<CharacterProperty>().resistMax, "resist");
		character.GetComponent<CharacterProperty>().resistMax = EditorGUILayout.IntField("resistMax",character.GetComponent<CharacterProperty>().resistMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().luck = EditorGUILayout.IntSlider("luck",character.GetComponent<CharacterProperty>().luck,0,character.GetComponent<CharacterProperty>().luckMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().luck / character.GetComponent<CharacterProperty>().luckMax, "luck");
		character.GetComponent<CharacterProperty>().luckMax = EditorGUILayout.IntField("luckMax",character.GetComponent<CharacterProperty>().luckMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().agile = EditorGUILayout.IntSlider("agile",character.GetComponent<CharacterProperty>().agile,0,character.GetComponent<CharacterProperty>().agileMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().agile / character.GetComponent<CharacterProperty>().agileMax, "agile");
		character.GetComponent<CharacterProperty>().agileMax = EditorGUILayout.IntField("agileMax",character.GetComponent<CharacterProperty>().agileMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().speed = EditorGUILayout.IntSlider("speed",character.GetComponent<CharacterProperty>().speed,0,character.GetComponent<CharacterProperty>().speedMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().speed / character.GetComponent<CharacterProperty>().speedMax, "speed");
		character.GetComponent<CharacterProperty>().speedMax = EditorGUILayout.IntField("speedMax",character.GetComponent<CharacterProperty>().speedMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().criticalChance = EditorGUILayout.IntSlider("criticalChance",character.GetComponent<CharacterProperty>().criticalChance,0,character.GetComponent<CharacterProperty>().criticalChanceMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().criticalChance / character.GetComponent<CharacterProperty>().criticalChanceMax, "criticalChance");
        character.GetComponent<CharacterProperty>().criticalChanceMax = EditorGUILayout.IntField("criticalChanceMax",character.GetComponent<CharacterProperty>().criticalChanceMax);
        DrawSpace();

        character.GetComponent<CharacterProperty>().vision = EditorGUILayout.IntSlider("vision",character.GetComponent<CharacterProperty>().vision,0,character.GetComponent<CharacterProperty>().visionMax);
		ProgressBar ((float)character.GetComponent<CharacterProperty>().vision / character.GetComponent<CharacterProperty>().visionMax, "vision");
        character.GetComponent<CharacterProperty>().visionMax = EditorGUILayout.IntField("visionMax",character.GetComponent<CharacterProperty>().visionMax);
        DrawSpace();
 
		m_object.ApplyModifiedProperties();
	}
	
	private void ProgressBar (float valor, string Label) 
	{
		Rect recta = GUILayoutUtility.GetRect (18, 18, "TextField");
		EditorGUI.ProgressBar (recta, valor, Label);
		EditorGUILayout.Space ();
	}

    void DrawSpace()
    {
        for(int i=0;i<3;i++)
        {
            EditorGUILayout.Space ();
        }
    }

	/*
	private void importorexport()
	{
		EditorGUILayout.BeginHorizontal();
		GUILayout.Label("File to Import:", GUILayout.MaxWidth(85));
		loadtext = EditorGUILayout.ObjectField(loadtext, typeof(Object), true,GUILayout.MaxWidth(250));

		GUILayout.Label("    Export to file Named: ", GUILayout.MaxWidth(145));
		newfilename = EditorGUILayout.TextField("",newfilename,GUILayout.MaxWidth(250));
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.BeginHorizontal();
		GUILayout.Label(" ", GUILayout.MaxWidth(85));
		if (GUILayout.Button("Load Stats", GUILayout.MaxWidth(230)))
		{
			if(loadtext is TextAsset)
			{
				getvalues();
				Debug.Log ("Done!");
			}
			else
			{
				Debug.Log ("the file is not a valid text file");
			}
		}

		GUILayout.Label(" ", GUILayout.MaxWidth(165));
		if (GUILayout.Button("Save Stats to file", GUILayout.MaxWidth(250)))
		{
			if(newfilename != null)
			{
				checkdirectory();
				createstring();
				System.IO.File.WriteAllText(Application.dataPath+"/RPG-Inator/StatsProfilesSaved/"+newfilename+".txt", statstosave);
				//Debug.Log (statstosave);
			}
		}
		EditorGUILayout.EndHorizontal();
	}
	private void saveelements()
	{
		character.GetComponent<charstats2D>().armorElement = (int)Armor_Element_type;
		character.GetComponent<charstats2D>().CloseWeaponElement = (int)CloseA_Element;
		character.GetComponent<charstats2D>().RangedWeaponElement = (int)RangeA_Element;
		character.GetComponent<charstats2D>().MagicalElement = (int)Magical_Element;
	}
	private void drawelementalitems()
	{
		EditorGUILayout.BeginHorizontal();
		GUILayout.Label("Armor Element Type:", GUILayout.MaxWidth(130));
		Armor_Element_type = (armor_element) EditorGUILayout.EnumPopup("",Armor_Element_type,GUILayout.MaxWidth(250));

		GUILayout.Label("                         Initial Life:", GUILayout.MaxWidth(170));
		character.GetComponent<charstats2D>().Initial_life = EditorGUILayout.IntField("", character.GetComponent<charstats2D>().Initial_life,GUILayout.MaxWidth(250));

		EditorGUILayout.EndHorizontal();

		EditorGUILayout.BeginHorizontal();
		GUILayout.Label("Close Weapon Element Type:", GUILayout.MaxWidth(172));
		CloseA_Element = (elements_atk) EditorGUILayout.EnumPopup("",CloseA_Element,GUILayout.MaxWidth(100));
		GUILayout.Label("Ranged Weapon Element Type:", GUILayout.MaxWidth(182));
		RangeA_Element = (elements_atk) EditorGUILayout.EnumPopup("",RangeA_Element,GUILayout.MaxWidth(100));
		GUILayout.Label("Magical Element Type:", GUILayout.MaxWidth(172));
		Magical_Element = (elements_atk) EditorGUILayout.EnumPopup("",Magical_Element,GUILayout.MaxWidth(100));
		EditorGUILayout.EndHorizontal();
	}



	/*
	private void getvalues()
	{
		string toread = loadtext.ToString();
		string[] lector;

		lector = toread.Split(","[0]);
		if(lector.Length !=0)
		{
			if(lector.Length !=18)
			{
				Debug.Log("the text file is not formatted correctly!");
			}
			else
			{
				for(int i=0; i<lector.Length; i++)
				{
					statstoload[i] = int.Parse(lector[i]);
				}
				assignimports();
			}
		}
	}

	private void assignimports()
	{
		character.GetComponent<charstats2D>().atk_bas = statstoload[0];
		character.GetComponent<charstats2D>().str = statstoload[1];
		character.GetComponent<charstats2D>().agi = statstoload[2];
		character.GetComponent<charstats2D>().vit = statstoload[3];
		character.GetComponent<charstats2D>().def_bas = statstoload[4];
		character.GetComponent<charstats2D>().def = statstoload[5];
		character.GetComponent<charstats2D>().Range_def = statstoload[6];
		character.GetComponent<charstats2D>().dex = statstoload[7];
		character.GetComponent<charstats2D>().luk = statstoload[8];
		character.GetComponent<charstats2D>().Int = statstoload[9];
		character.GetComponent<charstats2D>().Mdef_bas = statstoload[10];
		character.GetComponent<charstats2D>().Mdef = statstoload[11];
		character.GetComponent<charstats2D>().spirit = statstoload[12];
		character.GetComponent<charstats2D>().Initial_life = statstoload[13];
		character.GetComponent<charstats2D>().CloseWeaponElement = statstoload[14];
		character.GetComponent<charstats2D>().RangedWeaponElement = statstoload[15];
		character.GetComponent<charstats2D>().MagicalElement = statstoload[16];
		character.GetComponent<charstats2D>().armorElement = statstoload[17];
	}

	private void checkdirectory()
	{
		if(!Directory.Exists(Application.dataPath+"/RPG-Inator/StatsProfilesSaved"))
		{
			Directory.CreateDirectory(Application.dataPath+"/RPG-Inator/StatsProfilesSaved");
		}
	}
	private void createstring()
	{
				statstosave = ""+character.GetComponent<charstats2D>().atk_bas +","+
				character.GetComponent<charstats2D>().str +","+
				character.GetComponent<charstats2D>().agi +","+
				character.GetComponent<charstats2D>().vit +","+
				character.GetComponent<charstats2D>().def_bas +","+
				character.GetComponent<charstats2D>().def +","+
				character.GetComponent<charstats2D>().Range_def+","+
				character.GetComponent<charstats2D>().dex+","+
				character.GetComponent<charstats2D>().luk +","+
				character.GetComponent<charstats2D>().Int+","+
				character.GetComponent<charstats2D>().Mdef_bas +","+
				character.GetComponent<charstats2D>().Mdef +","+
				character.GetComponent<charstats2D>().spirit+","+
				character.GetComponent<charstats2D>().Initial_life+","+
				character.GetComponent<charstats2D>().CloseWeaponElement+","+
				character.GetComponent<charstats2D>().RangedWeaponElement+","+
				character.GetComponent<charstats2D>().MagicalElement+","+
				character.GetComponent<charstats2D>().armorElement ;
	}
	private void loadelements()
	{
		Armor_Element_type = (armor_element) character.GetComponent<charstats2D>().armorElement;
		CloseA_Element = (elements_atk) character.GetComponent<charstats2D>().CloseWeaponElement;
		RangeA_Element = (elements_atk) character.GetComponent<charstats2D>().RangedWeaponElement;
		Magical_Element = (elements_atk) character.GetComponent<charstats2D>().MagicalElement;
	}
	*/
}
